/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller.filemanager;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.ListIterator;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.swing.JTable;
import log2excel.model.OutgoingCallReport;

/**
 *
 * @author Wahyu Fahmy
 */
public class LogParser {
    
    /**** properties ****/
    private ReadFile readfile;
    public enum State {
        INIT,   // init
        HEADER, // find first separator, jump to header and then jump after one more separator
        READY,  // ready to read (after separator)
        IDLE    // find next separator, idle
    };
    private State parserState;
    
    private Pattern separatorPattern = Pattern.compile("-{5}");         // TRUE when at least "-" occurs 5 times
    private Pattern headerPattern = Pattern.compile("(SrN).+(STN)");    // TRUE when "SrN" and "STN" occurs
    private Pattern checkFstartPattern = Pattern.compile("(F\\*)");
    
    private boolean DEBUG_DEV = true;
    
    /*** methods ***/
    public LogParser(String filename){
        this.readfile = new ReadFile( filename );
    }
    
    public ArrayList<String[]> getAllReport(){
                
        ArrayList<String[]> result = new ArrayList<String[]>();
        ArrayList<String> content = this.readfile.getContent();
        
        this.parserState = State.INIT;
        ListIterator<String> litr = content.listIterator();
        
        int i = 0;
        while (litr.hasNext()) {
            System.out.println("LOOPING ke-" + i);
            i++;
            String contentstr = litr.next();
            String[] temp = contentstr.split("\\s+");
            
            if (DEBUG_DEV){
                for(int index_debug=0;index_debug<temp.length;index_debug++){
                    System.out.print("<TEMP[" +index_debug+ "]=" + temp[index_debug] + ">");
                }
                System.out.println("");
            }
            
            if (temp.length > 4) {
                
                if ( (this.parserState == State.IDLE) 
                        && (temp[4].equalsIgnoreCase("SrN")) && (temp[5].equalsIgnoreCase("STN")) ) {
                    // on IDLE
                    this.parserState = State.INIT;
                    if (DEBUG_DEV) System.out.println("look for another header");
                    
                } else if (this.parserState == State.READY) {
                    // on READY
                    Matcher m = separatorPattern.matcher(temp[4]);
                    if (! m.find()){
                        //@todo
                        String[] resulttemp = new String[10];
                        
                        Matcher fstart = checkFstartPattern.matcher(temp[7]);
                        if (! fstart.find()){
                            int index2 = 0;
                            for(int k=4;k<temp.length;k++){
                                if (temp[k] != null && !temp[k].equalsIgnoreCase("")){
                                    resulttemp[index2] = temp[k];
                                    System.out.println(temp[k]);
                                    index2++;
                                }
                            }
                            result.add(resulttemp);
                        } else {
                            int index2 = 0;
                            for(int k=4;k<temp.length;k++){
                                if (temp[k] != null && !temp[k].equalsIgnoreCase("")){
                                    if (k!=7){
                                        resulttemp[index2] = temp[k];
                                        System.out.println(temp[k]);
                                        index2++;
                                    } else {
                                        resulttemp[index2] = temp[k].substring(0, 18);
                                        resulttemp[index2+1] = temp[k].substring(18);
                                        System.out.println(temp[k]);
                                        System.out.println(temp[k+1]);
                                        index2 = index2 + 2;
                                    }
                                }
                            }
                            result.add(resulttemp);
                        }
                        if (DEBUG_DEV) System.out.println("baca satu");
                    } else {
                        this.parserState = State.IDLE; // idle
                        if (DEBUG_DEV) System.out.println("pindah ke idle");
                    }
                } else if(this.parserState == State.HEADER 
                        && (temp[4].equalsIgnoreCase("SrN")) && (temp[5].equalsIgnoreCase("STN")) ){
                    // on HEADER
                    this.parserState = State.READY;
                    System.out.println("pindah ke ready");
                    contentstr = litr.next();
                } else  if (this.parserState == State.INIT){
                    System.out.println("Masuk INIT");
                    System.out.println(temp[4]);
                    Matcher m = separatorPattern.matcher(temp[4]);
                    if (m.find()){
                        this.parserState = State.HEADER;
                        System.out.println("pindah ke header");
                    }
                }
            }
        }
        
        return result;
    }
    
    public ArrayList<OutgoingCallReport> createLogTable() throws ParseException{
        ArrayList<String[]> reports = this.getAllReport();
        ListIterator<String[]> iterate = reports.listIterator();
        ArrayList<OutgoingCallReport> retval = new ArrayList<OutgoingCallReport>();
        DateFormat df =new SimpleDateFormat("dd-MM-yyyy");
        DateFormat tf =new SimpleDateFormat("hh:mm:ss");
        while(iterate.hasNext()){
            String[] temp = iterate.next();
            Date d = df.parse(temp[4]); 
            Date t = tf.parse(temp[5]);
            OutgoingCallReport ocr = new OutgoingCallReport(Integer.parseInt(temp[0]),Integer.parseInt(temp[1]), Integer.parseInt(temp[2]), temp[3], df.parse(temp[4]), tf.parse(temp[5]), Integer.parseInt(temp[6]), Integer.parseInt(temp[7]), Double.parseDouble(temp[8]), temp[9]); 
            retval.add(ocr);
//            OutgoingCallReport.Records.add(ocr);
//            jTableModel4.insertRow(jTable5.getRowCount(),temp);
//            for(int j=0;j<temp.length;j++){
//                System.out.print(temp[j] + " ");
//            }
//            System.out.println("");
        }
        return retval;
    }
    
    public void createLogTable(javax.swing.table.DefaultTableModel jTableModel4, JTable jTable5){
        ArrayList<String[]> reports = this.getAllReport();
        ListIterator<String[]> iterate = reports.listIterator();
        while(iterate.hasNext()){
            String[] temp = iterate.next();
            jTableModel4.insertRow(jTable5.getRowCount(),temp);
            for(int j=0;j<temp.length;j++){
                System.out.print(temp[j] + " ");
            }
            System.out.println("");
        }
    }
    
    public void setTablewithRecords(ArrayList<OutgoingCallReport> data, javax.swing.table.DefaultTableModel jTableModel4, JTable jTable5){
        ArrayList<String[]> reports = this.getAllReport();
        ListIterator<String[]> iterate = reports.listIterator();
        while(iterate.hasNext()){
            String[] temp = iterate.next();
            jTableModel4.insertRow(jTable5.getRowCount(),temp);
            for(int j=0;j<temp.length;j++){
                System.out.print(temp[j] + " ");
            }
            System.out.println("");
        }
    }
    
    public static void main(String[] argv){
        LogParser lp = new LogParser("data/1st eternity dr 11.txt");
        ArrayList<String[]> test = lp.getAllReport();
        ListIterator<String[]> litr = test.listIterator();
        while (litr.hasNext()) {
            String[] contentstr = litr.next();
            System.out.println("EVERYTHING");
            for(int i=0;i<contentstr.length;i++){
                System.out.print(contentstr[i] + " ");
            }
            System.out.println("");
        }
    }
}